<template>
  <div>
    <h1>app组件</h1>
    <MyTable :data="goodsList">
      <template #header>
        <th>id</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #body="{ row, index }">
        <td>{{ index + 1 }}</td>
        <td>{{ row.goods_name }}</td>
        <td>$ {{ row.goods_price }}</td>
        <td>
          <input
            type="text"
            class="form-control form-control-sm form-ipt"
            v-if="row.inputVisible"
            @blur="row.inputVisible = false"
            v-focus
          />
          <button type="button" class="btn btn-primary btn-sm" v-else @click="row.inputVisible = true">+Tag</button>
          <span class="badge badge-warning ml-2" v-for="(item, index) in row.tags" :key="index">
            {{ item }}
          </span>
        </td>
        <td><button type="button" class="btn btn-danger btn-sm" @click="onRemove(index)">删除</button></td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from './components/my-table/MyTable.vue'
export default {
  name: 'MyApp',
  data() {
    return {
      goodsList: [],
    }
  },
  methods: {
    async getGoods() {
      const { data: res } = await this.$https.get('/api/goods')
      if (res.status !== 0) return console.log('数据请求失败!')
      else this.goodsList = res.data
      console.log(this.goodsList)
    },
    onRemove(id) {
      this.goodsList.splice(id, 1)
    },
  },
  directives: {
    focus: {
      mounted(el) {
        el.focus()
      },
    },
  },
  components: {
    MyTable,
  },
  created() {
    this.getGoods()
  },
}
</script>

<style lang="less" scoped>
.form-ipt {
  width: 60px;
  display: inline;
}
</style>
